//
//  Station.m
//  SZBus
//
//  Created by Kengi Chan on 10/26/09.
//  Copyright 2009 IDMT. All rights reserved.
//
#import "FMDatabase.h"

#import "Station.h"
#import "SZBusAppDelegate.h"
//#include <sqlite3.h>
//#import <Foundation/Foundation.h>


@implementation Station

@synthesize stationName, stationRoute, stationDirection, stationSort;

+ (NSMutableArray *) getDataForSearch:(NSString *)searchTerm {
	NSMutableArray *stationArray = [[NSMutableArray alloc] init];
	
	if([searchTerm length] == 0) return stationArray;	
	
	SZBusAppDelegate *appDelegate = (SZBusAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	FMDatabase *db = [FMDatabase databaseWithPath:[appDelegate getDBPath] ];
#ifdef DEBUG
	db.traceExecution =	YES;
#endif
	[db open];	
	FMResultSet *rs = [db executeQuery:@"select distinct name from station where name like ? order by name", 
						[NSString stringWithFormat:@"%%%@%%", searchTerm]	];
	while ([rs next]) {					
		Station *stationObj = [[Station alloc] init];
		stationObj.stationName = [rs stringForColumn:@"name"];
		[stationArray addObject:stationObj];
#ifdef DEBUG		
		NSLog(@"station %@",stationObj.stationName);
#endif
		[stationObj release];		
	}	
	[rs close];
	//[rs release];
	
	[db close];	
	//[db release];
	[pool release];
	return stationArray;
//	
}


- (void) dealloc {
	[stationName release];
	[stationRoute release];
	[stationDirection release];
	[stationSort release];
	[super dealloc];
}
@end
